<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<!-- photosurface.qdoc -->
  <title>Qt Quick Demo - Photo Surface | Qt 5.14</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="index.html">Qt 5.14</a></td><td >Qt Quick Demo - Photo Surface</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.14.2 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#running-the-example">Running the Example</a></li>
<li class="level1"><a href="#creating-the-main-window">Creating the Main Window</a></li>
<li class="level1"><a href="#accessing-folder-contents">Accessing Folder Contents</a></li>
<li class="level1"><a href="#displaying-images-on-the-photo-surface">Displaying Images on the Photo Surface</a></li>
<li class="level1"><a href="#handling-pinch-gestures">Handling Pinch Gestures</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Qt Quick Demo - Photo Surface</h1>
<span class="subtitle"></span>
<!-- $$$demos/photosurface-brief -->
<p>A QML app for touch devices that uses a Repeater with a <a href="../qtquick/qml-qt-labs-folderlistmodel-folderlistmodel.html">FolderListModel</a> to access content in a folder, and a <a href="../qtquick/qml-qtquick-pincharea.html">PinchArea</a> that contains a <a href="../qtquick/qml-qtquick-mousearea.html">MouseArea</a> to handle pinch gestures on the fetched content.</p>
<!-- @@@demos/photosurface -->
<!-- $$$demos/photosurface-description -->
<div class="descr"> <a name="details"></a>
<p class="centerAlign"><img src="images/qtquick-demo-photosurface-small.png" alt="" /></p><p><i>Photo Surface</i> demonstrates how to use a <a href="../qtquick/qml-qtquick-repeater.html">Repeater</a> with a <a href="../qtquick/qml-qt-labs-folderlistmodel-folderlistmodel.html">FolderListModel</a> and a <a href="../qtlabsplatform/qml-qt-labs-platform-filedialog.html">FileDialog</a> to access images from a folder selected by a user and how to handle dragging, rotation and pinch zooming within the same item using a <a href="../qtquick/qml-qtquick-pincharea.html">PinchArea</a> that contains a <a href="../qtquick/qml-qtquick-mousearea.html">MouseArea</a>.</p>
<p>All the app code is contained in one QML file, photosurface.qml. Inline JavaScript code is used to place, rotate, and scale images on the photo surface.</p>
<a name="running-the-example"></a>
<h4 id="running-the-example">Running the Example</h4>
<p>To run the example from <a href="http://doc.qt.io/qtcreator/index.html">Qt Creator</a>, open the <b>Welcome</b> mode and select the example from <b>Examples</b>. For more information, visit <a href="http://doc.qt.io/qtcreator/creator-build-example-application.html">Building and Running an Example</a>.</p>
<a name="creating-the-main-window"></a>
<h4 id="creating-the-main-window">Creating the Main Window</h4>
<p>To create the main window for the Photo Surface app, we use the <a href="../qt3d/qt3dextras-module.html#window">Window</a> QML type as the root item. It automatically sets up the window for use with <a href="../qtquick/qtquick-index.html">Qt Quick</a> graphical types:</p>
<pre class="cpp">

  <span class="type">Window</span> {
      <span class="name">id</span>: <span class="name">root</span>
      <span class="name">visible</span>: <span class="number">true</span>
      <span class="name">width</span>: <span class="number">1024</span>; <span class="name">height</span>: <span class="number">600</span>
      <span class="name">color</span>: <span class="string">&quot;black&quot;</span>
      property <span class="type">int</span> <span class="name">highestZ</span>: <span class="number">0</span>
      property <span class="type">real</span> <span class="name">defaultSize</span>: <span class="number">200</span>
      property <span class="type">var</span> <span class="name">currentFrame</span>: <span class="name">undefined</span>

</pre>
<p>To use the <a href="../qt3d/qt3dextras-module.html#window">Window</a> type, we must import it:</p>
<pre class="cpp">

  import <span class="type"><a href="../qtquick/qtquick-module.html">QtQuick</a></span><span class="operator">.</span>Window <span class="number">2.1</span>

</pre>
<a name="accessing-folder-contents"></a>
<h4 id="accessing-folder-contents">Accessing Folder Contents</h4>
<p>We use a <a href="../qtquick/qml-qtquick-repeater.html">Repeater</a> QML type together with the <a href="../qtquick/qml-qt-labs-folderlistmodel-folderlistmodel.html">FolderListModel</a> to display GIF, JPG, and PNG images located in a folder:</p>
<pre class="cpp">

          <span class="type"><a href="../qtquick/qml-qtquick-repeater.html">Repeater</a></span> {
              <span class="name">model</span>: <span class="name">FolderListModel</span> {
                  <span class="name">id</span>: <span class="name">folderModel</span>
                  <span class="name">objectName</span>: <span class="string">&quot;folderModel&quot;</span>
                  <span class="name">showDirs</span>: <span class="number">false</span>
                  <span class="name">nameFilters</span>: <span class="name">imageNameFilters</span>
              }

</pre>
<p>To use the <a href="../qtquick/qml-qt-labs-folderlistmodel-folderlistmodel.html">FolderListModel</a> type, we must import it:</p>
<pre class="cpp">

  import <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">.</span>labs<span class="operator">.</span>folderlistmodel <span class="number">1.0</span>

</pre>
<p>We use a <a href="../qtlabsplatform/qml-qt-labs-platform-filedialog.html">FileDialog</a> to enable users to select the folder that contains the images:</p>
<pre class="cpp">

      <span class="type"><a href="../qtlabsplatform/qml-qt-labs-platform-filedialog.html">FileDialog</a></span> {
          <span class="name">id</span>: <span class="name">fileDialog</span>
          <span class="name">title</span>: <span class="string">&quot;Choose a folder with some images&quot;</span>
          <span class="name">selectFolder</span>: <span class="number">true</span>
          <span class="name">folder</span>: <span class="name">picturesLocation</span>
          <span class="name">onAccepted</span>: <span class="name">folderModel</span>.<span class="name">folder</span> <span class="operator">=</span> <span class="name">fileUrl</span> <span class="operator">+</span> <span class="string">&quot;/&quot;</span>
      }

</pre>
<p>To use the <a href="../qtlabsplatform/qml-qt-labs-platform-filedialog.html">FileDialog</a> type, we must import <a href="../qtquickdialogs/qtquickdialogs-index.html">Qt Quick Dialogs</a>:</p>
<pre class="cpp">

  import <span class="type"><a href="../qtquick/qtquick-module.html">QtQuick</a></span><span class="operator">.</span>Dialogs <span class="number">1.0</span>

</pre>
<p>We use the <code>fileDialog.open()</code> function to open the file dialog when the app starts:</p>
<pre class="cpp">

  Component<span class="operator">.</span>onCompleted: fileDialog<span class="operator">.</span>open()

</pre>
<p>Users can also click the file dialog icon to open the file dialog. We use an <a href="../qdoc/09-qdoc-commands-includingimages.html#image">Image</a> QML type to display the icon. Inside the <a href="../qdoc/09-qdoc-commands-includingimages.html#image">Image</a> type, we use a <a href="../qtquick/qml-qtquick-mousearea.html">MouseArea</a> with the <code>onClicked</code> signal handler to call the <code>fileDialog.open()</code> function:</p>
<pre class="cpp">

      <span class="type"><a href="../qtquick/qml-qtquick-image.html">Image</a></span> {
          <span class="name">anchors</span>.top: <span class="name">parent</span>.<span class="name">top</span>
          <span class="name">anchors</span>.left: <span class="name">parent</span>.<span class="name">left</span>
          <span class="name">anchors</span>.margins: <span class="number">10</span>
          <span class="name">source</span>: <span class="string">&quot;resources/folder.png&quot;</span>
          <span class="type"><a href="../qtquick/qml-qtquick-mousearea.html">MouseArea</a></span> {
              <span class="name">anchors</span>.fill: <span class="name">parent</span>
              <span class="name">anchors</span>.margins: -<span class="number">10</span>
              <span class="name">onClicked</span>: <span class="name">fileDialog</span>.<span class="name">open</span>()
              <span class="name">hoverEnabled</span>: <span class="number">true</span>
              <span class="name">onPositionChanged</span>: {
                  <span class="name">tooltip</span>.<span class="name">visible</span> <span class="operator">=</span> <span class="number">false</span>
                  <span class="name">hoverTimer</span>.<span class="name">start</span>()
              }
              <span class="name">onExited</span>: {
                  <span class="name">tooltip</span>.<span class="name">visible</span> <span class="operator">=</span> <span class="number">false</span>
                  <span class="name">hoverTimer</span>.<span class="name">stop</span>()
              }

</pre>
<a name="displaying-images-on-the-photo-surface"></a>
<h4 id="displaying-images-on-the-photo-surface">Displaying Images on the Photo Surface</h4>
<p>We use a <a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a> as a delegate for a <a href="../qtquick/qml-qtquick-repeater.html">Repeater</a> to provide a frame for each image that the <a href="../qtquick/qml-qt-labs-folderlistmodel-folderlistmodel.html">FolderListModel</a> finds in the selected folder. We use JavaScript <code>Math()</code> methods to place the frames randomly on the photo surface and to rotate them at random angles, as well as to scale the images:</p>
<pre class="cpp">

              <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> {
                  <span class="name">id</span>: <span class="name">photoFrame</span>
                  <span class="name">width</span>: <span class="name">image</span>.<span class="name">width</span> <span class="operator">*</span> (<span class="number">1</span> <span class="operator">+</span> <span class="number">0.10</span> <span class="operator">*</span> <span class="name">image</span>.<span class="name">height</span> <span class="operator">/</span> <span class="name">image</span>.<span class="name">width</span>)
                  <span class="name">height</span>: <span class="name">image</span>.<span class="name">height</span> <span class="operator">*</span> <span class="number">1.10</span>
                  <span class="name">scale</span>: <span class="name">defaultSize</span> <span class="operator">/</span> <span class="name">Math</span>.<span class="name">max</span>(<span class="name">image</span>.<span class="name">sourceSize</span>.<span class="name">width</span>, <span class="name">image</span>.<span class="name">sourceSize</span>.<span class="name">height</span>)
                  Behavior on <span class="name">scale</span> { <span class="type"><a href="../qtquick/qml-qtquick-numberanimation.html">NumberAnimation</a></span> { <span class="name">duration</span>: <span class="number">200</span> } }
                  Behavior on <span class="name">x</span> { <span class="type"><a href="../qtquick/qml-qtquick-numberanimation.html">NumberAnimation</a></span> { <span class="name">duration</span>: <span class="number">200</span> } }
                  Behavior on <span class="name">y</span> { <span class="type"><a href="../qtquick/qml-qtquick-numberanimation.html">NumberAnimation</a></span> { <span class="name">duration</span>: <span class="number">200</span> } }
                  <span class="name">border</span>.color: <span class="string">&quot;black&quot;</span>
                  <span class="name">border</span>.width: <span class="number">2</span>
                  <span class="name">smooth</span>: <span class="number">true</span>
                  <span class="name">antialiasing</span>: <span class="number">true</span>
                  <span class="name">Component</span>.onCompleted: {
                      <span class="name">x</span> <span class="operator">=</span> <span class="name">Math</span>.<span class="name">random</span>() <span class="operator">*</span> <span class="name">root</span>.<span class="name">width</span> <span class="operator">-</span> <span class="name">width</span> <span class="operator">/</span> <span class="number">2</span>
                      <span class="name">y</span> <span class="operator">=</span> <span class="name">Math</span>.<span class="name">random</span>() <span class="operator">*</span> <span class="name">root</span>.<span class="name">height</span> <span class="operator">-</span> <span class="name">height</span> <span class="operator">/</span> <span class="number">2</span>
                      <span class="name">rotation</span> <span class="operator">=</span> <span class="name">Math</span>.<span class="name">random</span>() <span class="operator">*</span> <span class="number">13</span> <span class="operator">-</span> <span class="number">6</span>
                  }

</pre>
<a name="handling-pinch-gestures"></a>
<h4 id="handling-pinch-gestures">Handling Pinch Gestures</h4>
<p>We use a <a href="../qtquick/qml-qtquick-pincharea.html">PinchArea</a> that contains a <a href="../qtquick/qml-qtquick-mousearea.html">MouseArea</a> in the photo frames to handle dragging, rotation and pinch zooming of the frame:</p>
<pre class="cpp">

                  <span class="type"><a href="../qtquick/qml-qtquick-pincharea.html">PinchArea</a></span> {
                      <span class="name">anchors</span>.fill: <span class="name">parent</span>
                      <span class="name">pinch</span>.target: <span class="name">photoFrame</span>
                      <span class="name">pinch</span>.minimumRotation: -<span class="number">360</span>
                      <span class="name">pinch</span>.maximumRotation: <span class="number">360</span>
                      <span class="name">pinch</span>.minimumScale: <span class="number">0.1</span>
                      <span class="name">pinch</span>.maximumScale: <span class="number">10</span>
                      <span class="name">pinch</span>.dragAxis: <span class="name">Pinch</span>.<span class="name">XAndYAxis</span>
                      <span class="name">onPinchStarted</span>: <span class="name">setFrameColor</span>();

</pre>
<p>We use the <code>pinch</code> group property to control how the photo frames react to pinch gestures. The <code>pinch.target</code> sets <code>photoFrame</code> as the item to manipulate. The rotation properties specify that the frames can be rotated at all angles and the scale properties specify that they can be scaled between <code>0.1</code> and <code>10</code>.</p>
<p>In the <a href="../qtquick/qml-qtquick-mousearea.html">MouseArea</a>'s <code>onPressed</code> signal handler, we raise the selected photo frame to the top by increasing the value of its <code>z</code> property. The root item stores the z value of the top-most frame. The border color of the photo frame is controlled in the <code>onEntered</code> signal handler to highlight the selected image:</p>
<pre class="cpp">

                      <span class="type"><a href="../qtquick/qml-qtquick-mousearea.html">MouseArea</a></span> {
                          <span class="name">id</span>: <span class="name">dragArea</span>
                          <span class="name">hoverEnabled</span>: <span class="number">true</span>
                          <span class="name">anchors</span>.fill: <span class="name">parent</span>
                          <span class="name">drag</span>.target: <span class="name">photoFrame</span>
                          <span class="name">scrollGestureEnabled</span>: <span class="number">false</span>  <span class="comment">// 2-finger-flick gesture should pass through to the Flickable</span>
                          <span class="name">onPressed</span>: {
                              <span class="name">photoFrame</span>.<span class="name">z</span> <span class="operator">=</span> ++<span class="name">root</span>.<span class="name">highestZ</span>;
                              <span class="name">parent</span>.<span class="name">setFrameColor</span>();
                          }
                          <span class="name">onEntered</span>: <span class="name">parent</span>.<span class="name">setFrameColor</span>();

</pre>
<p>To enable you to test the example on the desktop, we use the <a href="../qtquick/qml-qtquick-mousearea.html">MouseArea</a>'s <code>onWheel</code> signal handler to simulate pinch gestures by using a mouse:</p>
<pre class="cpp">

                          <span class="name">onWheel</span>: {
                              <span class="keyword">if</span> (<span class="name">wheel</span>.<span class="name">modifiers</span> <span class="operator">&amp;</span> <span class="name">Qt</span>.<span class="name">ControlModifier</span>) {
                                  <span class="name">photoFrame</span>.<span class="name">rotation</span> <span class="operator">+=</span> <span class="name">wheel</span>.<span class="name">angleDelta</span>.<span class="name">y</span> <span class="operator">/</span> <span class="number">120</span> <span class="operator">*</span> <span class="number">5</span>;
                                  <span class="keyword">if</span> (<span class="name">Math</span>.<span class="name">abs</span>(<span class="name">photoFrame</span>.<span class="name">rotation</span>) <span class="operator">&lt;</span> <span class="number">4</span>)
                                      <span class="name">photoFrame</span>.<span class="name">rotation</span> <span class="operator">=</span> <span class="number">0</span>;
                              } <span class="keyword">else</span> {
                                  <span class="name">photoFrame</span>.<span class="name">rotation</span> <span class="operator">+=</span> <span class="name">wheel</span>.<span class="name">angleDelta</span>.<span class="name">x</span> <span class="operator">/</span> <span class="number">120</span>;
                                  <span class="keyword">if</span> (<span class="name">Math</span>.<span class="name">abs</span>(<span class="name">photoFrame</span>.<span class="name">rotation</span>) <span class="operator">&lt;</span> <span class="number">0.6</span>)
                                      <span class="name">photoFrame</span>.<span class="name">rotation</span> <span class="operator">=</span> <span class="number">0</span>;
                                  var scaleBefore = <span class="name">photoFrame</span>.<span class="name">scale</span>;
                                  <span class="name">photoFrame</span>.<span class="name">scale</span> <span class="operator">+=</span> <span class="name">photoFrame</span>.<span class="name">scale</span> <span class="operator">*</span> <span class="name">wheel</span>.<span class="name">angleDelta</span>.<span class="name">y</span> <span class="operator">/</span> <span class="number">120</span> <span class="operator">/</span> <span class="number">10</span>;
                              }
                          }

</pre>
<p>The <code>onWheel</code> signal handler is called in response to mouse wheel gestures. Use the vertical wheel to zoom and Ctrl and the vertical wheel to rotate frames. If the mouse has a horizontal wheel, use it to rotate frames.</p>
<p>Files:</p>
<ul>
<li><a href="qtdoc-demos-photosurface-main-cpp.html">demos/photosurface/main.cpp</a></li>
<li><a href="qtdoc-demos-photosurface-photosurface-pro.html">demos/photosurface/photosurface.pro</a></li>
<li><a href="qtdoc-demos-photosurface-photosurface-qml.html">demos/photosurface/photosurface.qml</a></li>
<li><a href="qtdoc-demos-photosurface-photosurface-qmlproject.html">demos/photosurface/photosurface.qmlproject</a></li>
<li><a href="qtdoc-demos-photosurface-photosurface-qrc.html">demos/photosurface/photosurface.qrc</a></li>
</ul>
<p>Images:</p>
<ul>
<li><a href="images/used-in-examples/demos/photosurface/resources/folder.png">demos/photosurface/resources/folder.png</a></li>
<li><a href="images/used-in-examples/demos/photosurface/resources/icon.png">demos/photosurface/resources/icon.png</a></li>
</ul>
</div>
<p><b>See also </b><a href="qmlapplications.html">QML Applications</a>.</p>
<!-- @@@demos/photosurface -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2020 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
